<?php
class visitacompComponent extends Component{
    protected $listActions = array(
        'Detalhes'       => "detalhes", 
        'Confirmar'      => "confirmar", 
        'Cancelar'       => "cancelar",
        'Comparecer'     => "comparecer",
        'Ausentar'       => "ausentar",
        'Matricular'     => "matricular",
        'Não Matricular' => "nmatricular"
     );
    
    public function __construct() {
        if(CURRENT_CONTROLLER == "evento"){
            $this->removeListAction('Cancelar');
            $this->removeListAction('Confirmar');
            $this->removeListAction('Matricular');
            $this->removeListAction('Não Matricular');
        }
        elseif(CURRENT_CONTROLLER == "visitacomp"){
            $this->removeListAction('Comparecer');
            $this->removeListAction('Ausentar');
            if(in_array(CURRENT_ACTION, array('aguarda','matriculado','nmatriculado'))){
                $this->removeListAction('Cancelar');
                $this->removeListAction('Confirmar');
            }else{
                $this->removeListAction('Matricular');
                $this->removeListAction('Não Matricular');
            }
        }
        parent::__construct();
    }
    public    $list_in_table = true;    
    public function format_telefone1($var){
        if($var == "") return "";
        return "(".substr($var, 0, 2).") ".substr($var, 2, 4) ."-".substr($var, 6, 8);
    }
    
    public function format_telefone2($var){
        return $this->format_telefone1($var);
    }

    protected function getActionLinks($model, $pkey, $item){
        if(CURRENT_CONTROLLER == "visitacomp"){
            if($item['venda_visitacomp_status']     == 'negado')     $rm = "Cancelar";
            elseif($item['venda_visitacomp_status'] == 'confirmado') $rm = "Confirmar";
        }elseif(CURRENT_CONTROLLER == "evento"){
            if($item['venda_visitacomp_status']     == 'compareceu') $rm = "Comparecer";
            elseif($item['venda_visitacomp_status'] == 'ausente')    $rm = "Ausentar";
        }
        
        if(isset($rm))$this->removeListAction($rm);
        $var = parent::getActionLinks($model, $pkey, $item);
        if(isset($rm))$this->addListAction($rm, strtolower($rm));
        return $var;
    }
    
    public function listInTable($model, $itens, $title = "", $class = '') {
        $this->axax();
        if(!empty($itens)) $this->search();
        parent::listInTable($model, $itens, $title, $class);
        
    }
    
    private function search(){
        echo "<input type='text' value='' id='search_inlist' placeholder='Pesquisar nesta lista'/>";
        $this->Html->LoadJqueryFunction("
            $('#search_inlist').keyup(function(){
                if( $(this).val() == ''){
                    $('#venda_visitacomp tbody>tr').show();
                    return false;
                }
                // Show only matching TR, hide rest of them
                $('#venda_visitacomp tbody>tr').hide();
                $('#venda_visitacomp td:contains-ci(\"' + $(this).val() + '\")').parent('tr').show();
            });
        ");

        $this->Html->LoadJsFunction('
            $.extend($.expr[":"],
            {
                "contains-ci": function(elem, i, match, array)
                    {
                            return (elem.textContent || elem.innerText || $(elem).text() || "").toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
                    }
            });
        ');
    }
    
    private function axax(){

        $this->Html->LoadJQueryFunction("
            $('#venda_visitacomp a.action_perm').click(function(){
                if($(this).hasClass('detalhes')){return true;}
                if($(this).hasClass('cancelar')){
                    if(!confirm('Deseja realmente cancelar a visita?')){
                        return false;
                    }
                }
                var url = $(this).attr('href'); 
                var row = $(this).parent().parent();
                $.ajax({
                    url: url,
                    dataType: 'json',
                    data: {ajax: true},
                    type: 'POST',
                    success: function(json){
                        if(json.status == '1'){
                            row.fadeOut('slow');
                        }else{
                            $('#erro').html(json.erro).fadeIn();
                        }
                    },
                    error: function(){
                        alert('Erro ao receber dados do servidor!');
                    }
                });
                return false;
            });
        ");
    }
}
?>